﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace DMU.Gui
{
    public partial class ucGiangVien : DevExpress.XtraEditors.XtraUserControl
    {
        public DataTable dtGV;
        public bool them;
        public DataTable dtBM;

        help hp = new help();
        Service sv = new Service();

        private DevExpress.XtraTab.XtraTabControl tab;

        public ucGiangVien(DevExpress.XtraTab.XtraTabControl tab)
        {
            InitializeComponent();
            this.tab = tab;
        }

        private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
                e.Value = e.ListSourceRowIndex + 1;
        }

        private void ucGiangVien_Load(object sender, EventArgs e)
        {
            dtGV = new DataTable();
            DataColumn[] cl = new DataColumn[9];
            cl[0] = new DataColumn("MaGV", Type.GetType("System.String"));
            cl[1] = new DataColumn("HoLot", Type.GetType("System.String"));
            cl[2] = new DataColumn("Ten", Type.GetType("System.String"));
            cl[3] = new DataColumn("MatKhau", Type.GetType("System.String"));
            cl[4] = new DataColumn("GioiTinh", Type.GetType("System.Boolean"));
            cl[5] = new DataColumn("NgaySinh", Type.GetType("System.String"));
            cl[6] = new DataColumn("MaBM", Type.GetType("System.String"));
            cl[7] = new DataColumn("IsActive", Type.GetType("System.Boolean"));
            cl[8] = new DataColumn("GhiChu", Type.GetType("System.String"));

            dtGV.Columns.AddRange(cl);
            DataColumn[] c=new DataColumn[1];
            c[0]=dtGV.Columns["MaGV"];

            dtGV.PrimaryKey = c;

            dtBM = sv.getDatatable("dmu_proPublicBoMonLoad", null);

            repositoryItemGridLookUpEdit2.DataSource = dtBM;
            repositoryItemGridLookUpEdit2.ValueMember = "MaBM";
            repositoryItemGridLookUpEdit2.DisplayMember = "TenBM";

            gridControl1.DataSource = dtGV;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            them = true;
            GuiPro.frmThemGiangVien frm = new GuiPro.frmThemGiangVien(this);
            frm.ShowDialog();
            
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            int rowHandle = gridView1.FocusedRowHandle;
            if (rowHandle >= 0)
            {
                try
                {
                    them = false;
                    GuiPro.frmThemGiangVien frm = new GuiPro.frmThemGiangVien(this);
                    frm.Text = "Bảng sửu chữa giang viên";

                    frm.txtMa.Text = gridView1.GetRowCellValue(rowHandle, "MaGV").ToString();
                    frm.txtHo.Text = gridView1.GetRowCellValue(rowHandle, "HoLot").ToString();
                    frm.txtTen.Text = string.Format("{0}", gridView1.GetRowCellValue(rowHandle, "Ten"));
                    frm.dteNgaySinh.Text = gridView1.GetRowCellValue(rowHandle, "NgaySinh").ToString();
                    frm.txtMK.Text = gridView1.GetRowCellValue(rowHandle, "MatKhau").ToString();
                    frm.chkNam.Checked = bool.Parse(gridView1.GetRowCellValue(rowHandle, "GioiTinh").ToString());
                    frm.dteNgaySinh.Text = gridView1.GetRowCellValue(rowHandle, "NgaySinh").ToString();
                    frm.lkpMaBM.EditValue = gridView1.GetRowCellValue(rowHandle, "MaBM").ToString();
                    frm.chkActive.Checked = bool.Parse(gridView1.GetRowCellValue(rowHandle, "IsActive").ToString());
                    frm.txtghiChu.Text = gridView1.GetRowCellValue(rowHandle, "GhiChu").ToString();

                    frm.ShowDialog();
                }
                catch (Exception) { }
            }
            else
                hp.showMes("Chọn dòng để thực hiện chức năng này !", -1);
        }

        private void btnLoadData_Click(object sender, EventArgs e)
        {
            dtGV = sv.getDatatable("dmu_proGVLoadData", null);
            gridControl1.DataSource = dtGV;

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int[] rowSelec = gridView1.GetSelectedRows(); int dem = 0; int len = rowSelec.Length;
            if (len > 0)
            {
                try
                {
                    if (hp.showMes(len + " đối tượng sẽ bị xóa ?", 2) == DialogResult.Yes)
                    {
                        sv.openConnect();
                        for (int i = 0; i < len; i++)
                        {
                            if (gridView1.GetRowCellValue(rowSelec[i], "MaGV") != null)
                            {
                                if (sv.cusTomExecuteScalarStore("dmu_proGVXoa", new SqlParameter("MaGV0", gridView1.GetRowCellValue(rowSelec[i], "MaGV").ToString()))!="1")
                                {
                                    gridView1.UnselectRow(rowSelec[i]);
                                    dem++;
                                }
                            }
                        }
                        sv.closeConnect();

                        gridView1.DeleteSelectedRows();
                        if (dem > 0)
                            hp.showMes(dem + " đối tượng không thể xóa !", 1);
                        else
                            hp.showMes("Xóa thành công " + len + " đối tượng", -1);
                    }
                }
                catch (Exception et)
                {
                    hp.showMes("Có lỗi khi xóa. Vui lòng thử lại ! " + et.Message, 1);
                }
            }
            else
            {
                hp.showMes("Chọn dòng để thực hiện chức năng này !!", -1);
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            tab.TabPages.RemoveAt(tab.SelectedTabPageIndex);
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                EventArgs et = new EventArgs();
                btnXoa_Click(sender, e);
            }

        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                EventArgs et = new EventArgs();
                btnSua_Click(sender, et);
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                EventArgs et = new EventArgs();
                btnSua_Click(sender, et);
            }
        }
    }
}
